/*
 * StoreFrame.java
 * Team qq 2011
 */
package com.google.code.timetrail.gui;


import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import com.google.code.timetrail.backend.Control;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Component;

public class StoreFrame extends JPanel implements DocumentListener{
    /**
     * 
     */
    private static final long serialVersionUID = -7890141237462582071L;
    private JLabel ammunitionLabel;
    private JLabel clothingLabel;
    private JPanel storePanel;
    private JLabel foodLabel;
    private JLabel enginesLabel;
    private JLabel priceAndWeightLabel;
    private JLabel itemNameLabel;
    private JLabel quantityLabel;
    private JLabel storeLabel;
    private JLabel fCapacitorLabel;
    private JLabel hullPartsLabel;
    private JLabel fuelCellsLabel;
    private JLabel enginePriceWeightLabel;
    private JLabel foodPriceWeightLabel;
    private JLabel clothingPriceWeightLabel;
    private JLabel ammunitionPriceWeightLabel;
    private JLabel fCapacitorsPriceWeightLabel;
    private JLabel hullPartsPriceWeightLabel;
    private JLabel fuelCellsPriceWeightLabel;
    private JTextField engineTextField;
    private JTextField foodTextField;
    private JTextField clothingTextField;
    private JTextField ammunitionTextField;
    private JTextField fCapacitorTextField;
    private JTextField hullPartsTextField;
    private JTextField fuelCellsTextField;
    private JLabel totalWeightLabel;
    private JLabel totalCostLabel;
    private JLabel moneyLabel;
    
    private JButton storeNextButton;
    private JButton storeBackButton;
    
    
    private StoreFrameBackend storeBackend;
    private Control myGameControl;
    private JPanel myCD;
    
    public StoreFrame(Control gameControl, JPanel cd){
        this.myGameControl = gameControl;
        this.myCD = cd;
        
        storeBackend = new StoreFrameBackend(gameControl);
        
        storePanel = new JPanel();
        
        storeLabel = new JLabel(storeBackend.getStoreLabelText());
        
        quantityLabel = new JLabel(storeBackend.getQuantityLabelText());
        
        itemNameLabel = new JLabel(storeBackend.getItemNameLabelText());
        
        priceAndWeightLabel = new JLabel(storeBackend.getPriceWeightLabelText());
        
        enginesLabel = new JLabel(storeBackend.getEngineLabelText());
        
        foodLabel = new JLabel(storeBackend.getFoodLabelText());
        
        clothingLabel = new JLabel(storeBackend.getClothingLabelText());
        
        ammunitionLabel = new JLabel(storeBackend.getAmmunitionLabelText());
        
        fCapacitorLabel = new JLabel(storeBackend.getFCapacitorLabelText());
        
        hullPartsLabel = new JLabel(storeBackend.getHullPartsLabelText());
        
        fuelCellsLabel = new JLabel(storeBackend.getFuelCellsLabelText());
        
        enginePriceWeightLabel = new JLabel(storeBackend.getEnginePriceWeight());
        
        foodPriceWeightLabel = new JLabel(storeBackend.getFoodPriceWeight());
        
        clothingPriceWeightLabel = new JLabel(storeBackend.getClothingPriceWeight());
        
        ammunitionPriceWeightLabel = new JLabel(storeBackend.getAmmunitionPriceWeight());
        
        fCapacitorsPriceWeightLabel = new JLabel(storeBackend.getFCapacitorsPriceWeight());
        
        hullPartsPriceWeightLabel = new JLabel(storeBackend.getHullPartsPriceWeight());
        
        fuelCellsPriceWeightLabel = new JLabel(storeBackend.getFuelCellsPriceWeight());
        
        engineTextField = new JTextField();
        engineTextField.setHorizontalAlignment(SwingConstants.CENTER);
        engineTextField.setText("0");
        engineTextField.setColumns(10);
        
        foodTextField = new JTextField();

        foodTextField.setHorizontalAlignment(SwingConstants.CENTER);
        foodTextField.setText("0");
        foodTextField.setColumns(10);
        
        clothingTextField = new JTextField();

        clothingTextField.setText("0");
        clothingTextField.setHorizontalAlignment(SwingConstants.CENTER);
        clothingTextField.setColumns(10);
        
        ammunitionTextField = new JTextField();

        ammunitionTextField.setText("0");
        ammunitionTextField.setHorizontalAlignment(SwingConstants.CENTER);
        ammunitionTextField.setColumns(10);
        
        fCapacitorTextField = new JTextField();

        fCapacitorTextField.setText("0");
        fCapacitorTextField.setHorizontalAlignment(SwingConstants.CENTER);
        fCapacitorTextField.setColumns(10);
        
        hullPartsTextField = new JTextField();

        hullPartsTextField.setText("0");
        hullPartsTextField.setHorizontalAlignment(SwingConstants.CENTER);
        hullPartsTextField.setColumns(10);
        
        fuelCellsTextField = new JTextField();

        fuelCellsTextField.setHorizontalAlignment(SwingConstants.CENTER);
        fuelCellsTextField.setText("0");
        fuelCellsTextField.setColumns(10);
        
        totalWeightLabel = new JLabel(storeBackend.getTotalWeightLabel());
        
        totalCostLabel = new JLabel(storeBackend.getTotalCostLabel());
        
        moneyLabel = new JLabel(storeBackend.getMoneyLabel());
        
        storeNextButton = new JButton(storeBackend.getNextButtonText());
        storeNextButton.addActionListener(new ActionListener() {
            

            public void actionPerformed(ActionEvent e) {
                
                
                
                

                
                storeBackend.pressNextButton();
                myCD.add(new TakingMoveFrame(myGameControl, myCD, "Store"), "name_131784086749183704");
                CardLayout cd = (CardLayout) myCD.getLayout();
                cd.last(myCD);

            }
        });
        
        storeBackButton = new JButton(storeBackend.getBackButtonText());
        storeBackButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                storeBackend.pressBackButton();
                CardLayout cd = (CardLayout) myCD.getLayout();
                cd.previous(myCD);
            }
        });
        GroupLayout gl_storePanel = new GroupLayout(storePanel);
        gl_storePanel.setHorizontalGroup(
        	gl_storePanel.createParallelGroup(Alignment.LEADING)
        		.addGroup(gl_storePanel.createSequentialGroup()
        			.addContainerGap(342, Short.MAX_VALUE)
        			.addComponent(storeBackButton)
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addComponent(storeNextButton)
        			.addContainerGap())
        		.addGroup(gl_storePanel.createSequentialGroup()
        			.addGap(113)
        			.addGroup(gl_storePanel.createParallelGroup(Alignment.LEADING)
        				.addGroup(gl_storePanel.createSequentialGroup()
        					.addGap(130)
        					.addComponent(storeLabel, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE))
        				.addGroup(gl_storePanel.createSequentialGroup()
        					.addComponent(moneyLabel, GroupLayout.PREFERRED_SIZE, 99, GroupLayout.PREFERRED_SIZE)
        					.addGap(31)
        					.addComponent(totalWeightLabel, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE))
        				.addGroup(gl_storePanel.createSequentialGroup()
        					.addGap(130)
        					.addComponent(totalCostLabel, GroupLayout.PREFERRED_SIZE, 98, GroupLayout.PREFERRED_SIZE))
        				.addGroup(gl_storePanel.createSequentialGroup()
        					.addGroup(gl_storePanel.createParallelGroup(Alignment.LEADING)
        						.addComponent(itemNameLabel, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE)
        						.addComponent(enginesLabel, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE)
        						.addComponent(foodLabel, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE)
        						.addComponent(clothingLabel, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE)
        						.addComponent(ammunitionLabel, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE)
        						.addComponent(fCapacitorLabel, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)
        						.addComponent(hullPartsLabel, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE)
        						.addComponent(fuelCellsLabel, GroupLayout.PREFERRED_SIZE, 99, GroupLayout.PREFERRED_SIZE))
        					.addGap(18)
        					.addGroup(gl_storePanel.createParallelGroup(Alignment.LEADING)
        						.addComponent(priceAndWeightLabel, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)
        						.addComponent(enginePriceWeightLabel, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)
        						.addComponent(foodPriceWeightLabel, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE)
        						.addComponent(clothingPriceWeightLabel, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE)
        						.addComponent(ammunitionPriceWeightLabel, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE)
        						.addComponent(fCapacitorsPriceWeightLabel, GroupLayout.PREFERRED_SIZE, 81, GroupLayout.PREFERRED_SIZE)
        						.addComponent(hullPartsPriceWeightLabel, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)
        						.addComponent(fuelCellsPriceWeightLabel, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE))
        					.addGap(18)
        					.addGroup(gl_storePanel.createParallelGroup(Alignment.LEADING)
        						.addGroup(gl_storePanel.createSequentialGroup()
        							.addGap(6)
        							.addGroup(gl_storePanel.createParallelGroup(Alignment.LEADING)
        								.addComponent(engineTextField, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
        								.addComponent(foodTextField, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
        								.addComponent(clothingTextField, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
        								.addComponent(ammunitionTextField, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
        								.addComponent(fCapacitorTextField, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
        								.addComponent(hullPartsTextField, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
        								.addComponent(fuelCellsTextField, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)))
        						.addComponent(quantityLabel, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE))))
        			.addContainerGap(173, Short.MAX_VALUE))
        );
        gl_storePanel.setVerticalGroup(
        	gl_storePanel.createParallelGroup(Alignment.TRAILING)
        		.addGroup(gl_storePanel.createSequentialGroup()
        			.addContainerGap(13, Short.MAX_VALUE)
        			.addComponent(storeLabel)
        			.addGap(28)
        			.addGroup(gl_storePanel.createParallelGroup(Alignment.LEADING)
        				.addComponent(itemNameLabel)
        				.addComponent(priceAndWeightLabel)
        				.addComponent(quantityLabel))
        			.addGroup(gl_storePanel.createParallelGroup(Alignment.LEADING)
        				.addGroup(gl_storePanel.createSequentialGroup()
        					.addGap(16)
        					.addGroup(gl_storePanel.createParallelGroup(Alignment.LEADING)
        						.addComponent(enginesLabel)
        						.addComponent(enginePriceWeightLabel))
        					.addGap(12)
        					.addGroup(gl_storePanel.createParallelGroup(Alignment.LEADING)
        						.addComponent(foodLabel)
        						.addComponent(foodPriceWeightLabel))
        					.addGap(12)
        					.addGroup(gl_storePanel.createParallelGroup(Alignment.LEADING)
        						.addComponent(clothingLabel)
        						.addComponent(clothingPriceWeightLabel))
        					.addGap(12)
        					.addGroup(gl_storePanel.createParallelGroup(Alignment.LEADING)
        						.addComponent(ammunitionLabel)
        						.addComponent(ammunitionPriceWeightLabel))
        					.addGap(12)
        					.addGroup(gl_storePanel.createParallelGroup(Alignment.LEADING)
        						.addComponent(fCapacitorLabel)
        						.addComponent(fCapacitorsPriceWeightLabel))
        					.addGap(12)
        					.addGroup(gl_storePanel.createParallelGroup(Alignment.LEADING)
        						.addGroup(gl_storePanel.createSequentialGroup()
        							.addComponent(hullPartsLabel)
        							.addGap(11)
        							.addComponent(fuelCellsLabel))
        						.addGroup(gl_storePanel.createSequentialGroup()
        							.addComponent(hullPartsPriceWeightLabel)
        							.addGap(11)
        							.addComponent(fuelCellsPriceWeightLabel)))
        					.addGap(25)
        					.addGroup(gl_storePanel.createParallelGroup(Alignment.LEADING)
        						.addComponent(moneyLabel)
        						.addComponent(totalWeightLabel))
        					.addGap(12)
        					.addComponent(totalCostLabel))
        				.addGroup(gl_storePanel.createSequentialGroup()
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addComponent(engineTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        					.addComponent(foodTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        					.addComponent(clothingTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        					.addComponent(ammunitionTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        					.addComponent(fCapacitorTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        					.addGroup(gl_storePanel.createParallelGroup(Alignment.LEADING)
        						.addGroup(gl_storePanel.createSequentialGroup()
        							.addGap(27)
        							.addComponent(fuelCellsTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        						.addComponent(hullPartsTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
        			.addPreferredGap(ComponentPlacement.UNRELATED)
        			.addGroup(gl_storePanel.createParallelGroup(Alignment.BASELINE)
        				.addComponent(storeNextButton)
        				.addComponent(storeBackButton))
        			.addContainerGap())
        );
        gl_storePanel.linkSize(SwingConstants.HORIZONTAL, new Component[] {itemNameLabel, enginesLabel, foodLabel, clothingLabel, ammunitionLabel, fCapacitorLabel, hullPartsLabel, fuelCellsLabel});
        gl_storePanel.linkSize(SwingConstants.HORIZONTAL, new Component[] {priceAndWeightLabel, enginePriceWeightLabel, foodPriceWeightLabel, clothingPriceWeightLabel, ammunitionPriceWeightLabel, fCapacitorsPriceWeightLabel, hullPartsPriceWeightLabel, fuelCellsPriceWeightLabel});
        storePanel.setLayout(gl_storePanel);
        
        engineTextField.getDocument().addDocumentListener(this);
        foodTextField.getDocument().addDocumentListener(this);
        clothingTextField.getDocument().addDocumentListener(this);
        ammunitionTextField.getDocument().addDocumentListener(this);
        fCapacitorTextField.getDocument().addDocumentListener(this);
        hullPartsTextField.getDocument().addDocumentListener(this);
        fuelCellsTextField.getDocument().addDocumentListener(this);
        GroupLayout groupLayout = new GroupLayout(this);
        groupLayout.setHorizontalGroup(
        	groupLayout.createParallelGroup(Alignment.LEADING)
        		.addGroup(groupLayout.createSequentialGroup()
        			.addContainerGap()
        			.addComponent(storePanel, GroupLayout.DEFAULT_SIZE, 588, Short.MAX_VALUE)
        			.addContainerGap())
        );
        groupLayout.setVerticalGroup(
        	groupLayout.createParallelGroup(Alignment.LEADING)
        		.addGroup(groupLayout.createSequentialGroup()
        			.addContainerGap()
        			.addComponent(storePanel, GroupLayout.DEFAULT_SIZE, 411, Short.MAX_VALUE)
        			.addContainerGap())
        );
        setLayout(groupLayout);
    }

    @Override
    public void changedUpdate(DocumentEvent arg0) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void insertUpdate(DocumentEvent e) {

            storeBackend.setEngineTextField(engineTextField.getText());
            engineTextField.setBackground(storeBackend.getEngineTextFieldColor());
            
           

            storeBackend.setfoodTextField(foodTextField.getText());
            foodTextField.setBackground(storeBackend.getFoodTextFieldColor());
            
            

            storeBackend.setClothingTextField(clothingTextField.getText());
            clothingTextField.setBackground(storeBackend.getClothingTextFieldColor());

            storeBackend.setAmmunitionTextField(ammunitionTextField.getText());
            ammunitionTextField.setBackground(storeBackend.getAmmunitionTextFieldColor());

            storeBackend.setFCapacitorTextField(fCapacitorTextField.getText());
            fCapacitorTextField.setBackground(storeBackend.getFCapacitorTextFieldColor());

            storeBackend.setHullPartsTextField(hullPartsTextField.getText());
            hullPartsTextField.setBackground(storeBackend.getHullPartsTextFieldColor());

            storeBackend.setFuelCellsTextField(fuelCellsTextField.getText());
            fuelCellsTextField.setBackground(storeBackend.getFuelCellsTextFieldColor());

        
        engineTextField.setBackground(storeBackend.getEngineTextFieldColor());
        foodTextField.setBackground(storeBackend.getFoodTextFieldColor());
        clothingTextField.setBackground(storeBackend.getClothingTextFieldColor());
        ammunitionTextField.setBackground(storeBackend.getAmmunitionTextFieldColor());
        fCapacitorTextField.setBackground(storeBackend.getFCapacitorTextFieldColor());
        hullPartsTextField.setBackground(storeBackend.getHullPartsTextFieldColor());
        fuelCellsTextField.setBackground(storeBackend.getFuelCellsTextFieldColor());
        
        storeNextButton.setEnabled(storeBackend.canEnableNextButton());
        totalWeightLabel.setText(storeBackend.getTotalWeightLabel());
        totalCostLabel.setText(storeBackend.getTotalCostLabel());
    }
    @Override
    public void removeUpdate(DocumentEvent e) {


            storeBackend.setEngineTextField(engineTextField.getText());
            engineTextField.setBackground(storeBackend.getEngineTextFieldColor());
            
           

            storeBackend.setfoodTextField(foodTextField.getText());
            foodTextField.setBackground(storeBackend.getFoodTextFieldColor());
            

            storeBackend.setClothingTextField(clothingTextField.getText());
            clothingTextField.setBackground(storeBackend.getClothingTextFieldColor());

            storeBackend.setAmmunitionTextField(ammunitionTextField.getText());
            ammunitionTextField.setBackground(storeBackend.getAmmunitionTextFieldColor());

            storeBackend.setFCapacitorTextField(fCapacitorTextField.getText());
            fCapacitorTextField.setBackground(storeBackend.getFCapacitorTextFieldColor());

            storeBackend.setHullPartsTextField(hullPartsTextField.getText());
            hullPartsTextField.setBackground(storeBackend.getHullPartsTextFieldColor());

            storeBackend.setFuelCellsTextField(fuelCellsTextField.getText());
            fuelCellsTextField.setBackground(storeBackend.getFuelCellsTextFieldColor());

        
        engineTextField.setBackground(storeBackend.getEngineTextFieldColor());
        foodTextField.setBackground(storeBackend.getFoodTextFieldColor());
        clothingTextField.setBackground(storeBackend.getClothingTextFieldColor());
        ammunitionTextField.setBackground(storeBackend.getAmmunitionTextFieldColor());
        fCapacitorTextField.setBackground(storeBackend.getFCapacitorTextFieldColor());
        hullPartsTextField.setBackground(storeBackend.getHullPartsTextFieldColor());
        fuelCellsTextField.setBackground(storeBackend.getFuelCellsTextFieldColor());
        
        storeNextButton.setEnabled(storeBackend.canEnableNextButton());
        totalWeightLabel.setText(storeBackend.getTotalWeightLabel());
        totalCostLabel.setText(storeBackend.getTotalCostLabel());
    }
}
